// /pages/movie/movie.js
var app = getApp();

Page({
  data: {
    showRating: false,
    showWish: false,
    acquiredSelected: false,  // 精选榜单只请求一次
    inTheaters: {},   // 影院热映
    comingSoon: {},    // 即将上映
    top250: {},        // 豆瓣Top250
    weekly: {},        // 口碑榜
    newMovies: {},       //  新片榜
    usBox: {}            // 票房榜
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var inTheatersURL = app.globalData.doubanBase + app.globalData.inTheaters + "?start=0&&count=8";
    var comingSoonURL = app.globalData.doubanBase + app.globalData.comingSoon + "?start=0&&count=8";

    this.getMovieListData(inTheatersURL, "inTheaters", "影院热映");
    this.getMovieListData(comingSoonURL, "comingSoon", "即将上映");

    this.getSelectedListData();
  },
  onShow: function () {
    // 页面显示
  },
  /** 获取电影列表 */
  getMovieListData: function (url, settedKey, categoryTitle) {
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    });
    var that = this;
    // 请求电影数据
    wx.request({
      url: url,
      method: 'GET',
      header: {"content-type": "json"},
      success: function (res) {
        // 组装电影数据
        var data = res.data;
        that.processMovieListData(data, settedKey, categoryTitle);
      },
      complete: function () {
        wx.hideToast();
      }
    })
  },
  /** 组装电影数据 */
  processMovieListData: function (data, settedKey, categoryTitle) {
    var movies = [];
    for (let idx in data.subjects) {
      var subject = data.subjects[idx];
      var showRating = false;
      var showWish = false;
      if ("inTheaters" == settedKey) {
        showRating = true;
        showWish = false;
      } else {
        showRating = true;
        showWish = false;
      }
      var temp = {
        id: subject.id,
        title: subject.title,
        rating: subject.rating,
        collect_count: subject.collect_count,
        images: subject.images,
        subtype: subject.subtype,
        directors: subject.directors,
        casts: subject.casts,
        year: subject.year,
        showRating: showRating,
        showWish: showWish
      };
      movies.push(temp);
    }
    var readyData = {};
    readyData[settedKey] = {
      categoryTitle: categoryTitle,
      movies: movies
    };
    this.setData(readyData);
  },
  /** 获取电影榜单数据 */
  getSelectedListData: function () {
    var that = this;
    // 豆瓣口碑榜，新片榜是高级接口，票房榜不可用，这里用豆瓣Top250数据 
    var top250URL = app.globalData.doubanBase + app.globalData.top250 + "?start=0&count=3";
    var weeklyURL = app.globalData.doubanBase + app.globalData.weekly + "?start=0&count=3";
    var newMoviesURL = app.globalData.doubanBase + app.globalData.newMovies + "?start=0&count=3";
    var usBoxURL = app.globalData.doubanBase + app.globalData.usBox + "?start=0&count=3";
    if (!that.data.acquiredSelected) {
      var readyData = {};
      readyData["acquiredSelected"] = {
        "acquiredSelected": true
      }
      that.setData(readyData);
      wx.showToast({
        title: '加载中',
        icon: 'loading',
        duration: 10000
      });
      that.getSelectedListDataByType(top250URL, "top250");
      that.getSelectedListDataByType(weeklyURL, "weekly");
      that.getSelectedListDataByType(newMoviesURL, "newMovies");
      that.getSelectedListDataByType(usBoxURL, "usBox");
    }
  },
  getSelectedListDataByType: function(url, type){
    var that = this;
    wx.request({
      url: url,
      header: { 'content-type': "json" },
      success: function (res) {
        var data = res.data;
        if (type == 'usBox' || type == 'weekly') {
          var temp = [];
          data.subjects.forEach(function (it) {
            temp.push(it['subject']);
          });
          data.subjects = temp;
        }
        that.processSelectedListData(data, type);
      }
    });
  },
  /** 组装榜单数据 */
  processSelectedListData: function (data, type) {
    var movieList = [];
    for (let idx in data.subjects) {
      var subject = data.subjects[idx];
      var temp = {
        id: subject.id,
        title: subject.title,
        rating: subject.rating,
        collectCount: subject.collect_count,
        images: subject.images,
        subtype: subject.subtype,
        directors: subject.directors,
        casts: subject.casts,
        year: subject.year
      };
      movieList.push(temp);
    }
    switch (type){
      case "top250" : {
        this.setData({ 'top250': { categoryType: "top250", categoryTitle: "豆瓣Top250", desc: "8分以上好电影", movies: movieList}});
        break;
      }
      case "weekly": {
        this.setData({ 'weekly': { categoryType: "weekly", categoryTitle: "口碑榜", desc: "口碑不错的好电影", movies: movieList } });
        break;
      }
      case "newMovies": {
        this.setData({ 'newMovies': { categoryType: "newMovies", categoryTitle: "新片榜", desc: "豆瓣新片榜", movies: movieList } });
        break;
      }
      case "usBox": {
        this.setData({ 'usBox': { categoryType: "usBox", categoryTitle: "北美票房榜", desc: data['date'], movies: movieList } });
        break;
      }
    }
  },
  /** 搜索电影 */
  bindSearchNavigate: function (event) {
    wx.navigateTo({
      url: '/pages/my/movie/search/search'
    })
  },
  /** 显示更多电影列表 */
  bindMore: function (event) {
    var typeId = event.currentTarget.dataset.typeId;
    wx.navigateTo({
      url: '/pages/my/movie/movie-more/movie-more?typeId=' + typeId
    });
  },
  /** 跳转到榜单列表 */
  bindSelected: function (event) {
    var typeId = event.currentTarget.dataset.typeId;
    var typeTitle = event.currentTarget.dataset.typeTitle;
    wx.navigateTo({
      url: '/pages/my/movie/selected/selected?typeId=' + typeId + "&&typeTitle=" + typeTitle
    });
  },
  /** 跳转电影详情页 */
  bindMovieDetail: function (event) {
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/my/movie/movie-detail/movie-detail?id=' + id
    });
  }
})